home *** CD-ROM | disk | FTP | other *** search
/ Your Choice 3 / Your Choice Software Collection 3.iso / prgmming / swag05 / numbers.swg < prev    next >
Text File  |  1994-09-22  |  9KB  |  1 lines

  1. SWAGOLX.EXE (c) 1993 GDSOFT  ALL RIGHTS RESERVED 00006                                                                           1      05-25-9408:00ALL                      MIKE ANTTILA             writing bits..           SWAG9405            8      ₧8   {π MT>   Could someone please tell me how to write to/read from a particularπ MT>  bit in a number?  Do you have to break the number down into binaryπ MT>  or is there some function somewhere that I haven't found yet?ππHere's some procs I wrote that should help you out:π}ππProcedure SetBit(Var Number : Byte; Bit : Byte);π π Beginπ  Number := Number OR (1 SHL Bit);π End;π πProcedure ClearBit(Var Number : Byte; Bit : Byte);π π Beginπ  Number := Number AND NOT (1 SHL Bit);π End;π πFunction ReadBit(Number, Bit : Byte) : Boolean;π π Beginπ  ReadBit := (Number AND (1 SHL Bit)) <> 0;π End;π{πOK, provided you know binary, this should be pretty simple to implement.  Theπbits are of course numbered 7-0.  SetBit sets a given bit to 1, ClearBit sets aπgiven bit to 0, and ReadBit returns TRUE if 1, FALSE if 0.  Anyway, hope thatπhelps...ππ                                      PsychoMan.π}π       2      05-25-9408:08ALL                      VARIOUS                  Improved Decimal To BinarSWAG9405            7      ₧8   π{Convert a Decimal to a String - Maximum number of bits = 16}ππFunction Dec2Bin (D: Word; No_Bits: Byte): String;πVar A   : Word;π    L   : Byte;π    S   : String;πBeginπ   S := '';π   A := Trunc (Exp ((No_Bits-1)*Ln (2)));π   For L := No_Bits downto 1 doπ   Beginπ      A := A div 2;π      If (D AND A)=A then S := S+'1' else S := S+'0';π   End;π   Dec2Bin := S;πEnd;ππ(*------------------------------------------------------*)πFunction BinStr(num:word;bits:byte):string; assembler;πASMπ      PUSHFπ      LES  DI, @Resultπ      XOR  CH, CHπ      MOV  CL, bitsπ      MOV  ES:[DI], CLπ      JCXZ @@3π      ADD  DI, CXπ      MOV  BX, numπ      STDπ@@1:  MOV  AL, BLπ      AND  AL, $01π      OR   AL, $30π      STOSBπ      SHR  BX, 1π      LOOP @@1π@@3:  POPFπEnd;ππ    3      05-25-9408:15ALL                      MAYNARD PHILBROOK        Reading on bit of an inteSWAG9405            10     ₧8   {π SK> 12345 --------------- Longinteger of the value 12345π SK> ^^^^^π SK> |||||π SK> ||||+----------------- Integer value 5π SK> ||||π SK> |||+------------------ Integer value 4π SK> |||π SK> ||+------------------- Integer value 3π SK> ||π SK> |+-------------------- Integer value 2π SK> |π SK> +--------------------- Integer value 1ππ SK> I tried using the procedure of geting the MOD of a number then div theπ SK> number out. It works fine until you get a number likeπ SK> 10,100,1000,100000, etc....π SK> Please help...π not sure what your asking but have you  can use SHR, SHL, OR ect to fetchπ single bits..........π}πfunction getbitstate( bitpos:byte; lint:longint):boolean;π beginπ  asmπ   mov @result, 00; { clear bolean first }π   cmp bitpos, 16π   jg  @higher;π   mov bx, lint;π@yup:π   test bx, bitpos;π   jnz @yes;π   jmp @done;π@higher:π   mov bx,lint+2;π   jmp @yup;π@yes:π   inc @result, 1;          { adjust bolean return }π@done:π  end;πend;ππ_____ to use it ____ππBeginπ if getbitstate(8, $80) then Write(' Yup, it's on ');πend;π                                                                                                       4      05-25-9408:21ALL                      EDWIN GROOTHUIS          RIP                      SWAG9405            12     ₧8   {π AE> numbersystem... When you want to create something in RIP youπ AE> first need to write some calculator to translate normal numbersπ AE> to RIP codes...ππIt's not that difficult, you know how to convert hex -> normal andπnormal -> hex? Well, then you also can convert mega -> normal and normalπ-> megaππlittle idea:π}ππfunction word2mega(w:word):string;πconst    table:array[0..35] of char='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';πvar      s:string;πbeginπ  s:='';π  while w>0 doπ  beginπ    s:=table[w mod 36]+s;π    w:=w div 36;π  end;π  while length(s)<4 do s:='0'+s;π  word2mega:=s;πend;ππfunction mega2word(s:string):word;πvar      w:word;πbeginπ  w:=0;π  if length(s)<5 thenπ    while s<>'' doπ    beginπ      if s[1]>'9' thenπ        w:=w*36+ord(s[1])-ord('A')+10π      elseπ        w:=w*36+ord(s[1])-ord('0');π      delete(s,1,1);π    end;π  mega2word:=w;πend;πππvar    n:word;π        s:string;πbeginπ  s:=paramstr(1);π  for n:=1 to length(s) doπ    s[n]:=upcase(s[n]);π  writeln('mega2word: ',mega2word(s));π  val(s,n,n);π  writeln('word2mega: ',word2mega(n));πend.ππconverts a meganum to a word and a word to a meganum in one program!π(Just one program so I don't have to think in which way it has to beπconverted)ππmega 12<cr> givesπmega2word: 38πword2mega: 0Cππmega 1C<cr> givesπmega2word: 48πword2mega: 00ππ                                                                                   5      05-25-9408:21ALL                      JASON KING               RIP Mega Numbers         SWAG9405            14     ₧8   π{πYou're right about that... The only thin why I found it difficult, isπbecause TP (or any other language) doesn't support the MenaNum itself..πSome other thing is that when you're creating a file, you need to useπtwo windows, and constantly convert the numbers... But for the source,πthanks, I'll look it over... Is it Ok with you when I place it in theπdownload of my BBS..? I havn't seen any DEC<>MEGA program yet...ππTry this...π}ππFunction MegaToDec(Num: String) : LongInt; {Converts String MEGA to Dec}πConst MegaNum : Set of Char = ['0'..'9','A'..'Z']; {assume UC}ππVar HoldNum,π    TempVal : LongInt;π    CharPos : Byte; {Position of Character}ππ    Function ToThirtySix(Ex: Byte) : Longint; {Raises to power of 36}π    Var Times: Byte;π        HoldPower: LongInt;ππ    Beginπ        HoldPower:=0;π        If Ex=0 then beginπ           ToThirtySix:=1;π           End;π        For Times:=1 to Ex do HoldPower:=HoldPower*36;π        ToThirtySix:=HoldPower;π    End;ππ   Function ConvertVal(Ch: Char) : Byte;π   Var Temp : Char;π   Beginπ        Temp:=Ch;π        Upcase(Temp);π        If Ord(Ch)>47 and Ord(Ch)<58 then ConvertVal:=Ord(Ch)-48;π                {Converts if 0..9}π        If Ord(Ch)>64 and Ord(Ch)<91 then ConvertVal:=Ord(Ch)-55;π   End;ππ   Beginπ        HoldNum:=0;π        For CharPos:=Length(Num) downto 1 doπ            HoldNum:=HoldNum+ConverVal(Num[CharPos])*π                ToThirtysix(CharPos-1);π        MegaToDec:=HoldNum;π   End;ππNote: this is untested, but it should work... try values of 10 Mega π(should by 36 dec) or 2Z (should be 107 dec I think)... Tell me how itπworks...π                                                  6      05-26-9406:16ALL                      DAVID DUNSON             Binary and Hexidecimal   SWAG9405            16     ₧8   {πI've seen requests for these two procedures several times, and finally gotπaround to writing them in ASM.ππ{ ------- CUT HERE ------- }ππ(* Hex converts a number (num) to Hexadecimal.                      *)π(*    num  is the number to convert                                 *)π(*    nib  is the number of Hexadecimal digits to return            *)π(* Example: Hex(31, 4) returns '001F'                               *)ππFunction Hex(num: Word; nib: Byte): String; Assembler;πASMπ      PUSHFπ      LES  DI, @Resultπ      XOR  CH, CHπ      MOV  CL, nibπ      MOV  ES:[DI], CLπ      JCXZ @@3π      ADD  DI, CXπ      MOV  BX, numπ      STDπ@@1:  MOV  AL, BLπ      AND  AL, $0Fπ      OR   AL, $30π      CMP  AL, $3Aπ      JB   @@2π      ADD  AL, $07π@@2:  STOSBπ      SHR  BX, 1π      SHR  BX, 1π      SHR  BX, 1π      SHR  BX, 1π      LOOP @@1π@@3:  POPFπEnd;πππ(* Binary converts a number (num) to Binary.                        *)π(*    num  is the number to convert                                 *)π(*    bits is the number of Binary digits to return                 *)π(* Example: Binary(31, 16) returns '0000000000011111'               *)ππFunction Binary(num: Word; bits: Byte): String; Assembler;πASMπ      PUSHFπ      LES  DI, @Resultπ      XOR  CH, CHπ      MOV  CL, bitsπ      MOV  ES:[DI], CLπ      JCXZ @@3π      ADD  DI, CXπ      MOV  BX, numπ      STDπ@@1:  MOV  AL, BLπ      AND  AL, $01π      OR   AL, $30π      STOSBπ      SHR  BX, 1π      LOOP @@1π@@3:  POPFπEnd;ππ{ ------- CUT HERE ------- }ππThese procedures are fully optomized to my knowledge and have been testedπagainst normal Pascal routines that perform the same functions.  Test resultsπreturned that Hex performed aprox. 2.14 times faster than it's Pascalπequivilent, and Binary performed aprox. 14 times faster than it's Pascalπequivilent.ππEnjoy!πDavidπ